﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebTV_Kickstarter;

namespace Kickstarter.Controllers
{
    public class DonationController : Controller
    {

        // POST: /Donation/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.

        public ActionResult Donate(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            if (Session["Gebruiker"] != null)
            {
                Project project = Project.FindProject(id);
                if (project == null)
                {
                    return HttpNotFound();
                }
                ViewBag.ProjectName = Project.FindProject(id).Title;
                ViewBag.ProjectID = Project.FindProject(id).Id;
                Donation d = new Donation();
                d.ProjectId = (int)id;
                d.User = (Gebruiker)Session["Gebruiker"];
                return View(d);
            }
            else
            {
                return RedirectToAction("../Donation/Anoniem/"+id);
            }
        }
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Donate([Bind(Include="Id,Amount,UserId,ProjectID")] Donation donation, int? id)
        {
            ViewBag.ProjectName = Project.FindProject(id).Title;
            ViewBag.ProjectID = Project.FindProject(id).Id;
            if (donation.Amount > 0)
            {
                if (ModelState.IsValid)
                {
                    if (Session["Gebruiker"] != null)
                    {

                        Donation.AddDonationFull(donation, id);
                        
                        
                        //Returneer naar index pagina
                        return RedirectToAction("ThankYou/"+donation.ProjectId, "Home");
                    }
                }
            }
            return View(donation);
        }

        public ActionResult Anoniem(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ViewBag.ProjectName = Project.FindProject(id).Title;
            ViewBag.ProjectID = Project.FindProject(id).Id;
            if (Session["Gebruiker"] == null)
            {
                Project project = Project.FindProject(id);
                if (project == null)
                {
                    return HttpNotFound();
                }
                ViewBag.ProjectName = Project.FindProject(id).Title;
                ViewBag.ProjectID = Project.FindProject(id).Id;
                AnoDonation d = new AnoDonation();
                d.ProjectId = (int)id;
                return View(d);
            }
            else
            {
                return RedirectToAction("../Donation/Donate/" + id);
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Anoniem([Bind(Include = "Id,Amount,ProjectID,Agree")] AnoDonation donation, int? id)
        {
            ViewBag.ProjectName = Project.FindProject(id).Title;
            ViewBag.ProjectID = Project.FindProject(id).Id;
            if (donation.Amount > 0)
            {
                if (ModelState.IsValid)
                {
                    if (Session["Gebruiker"] == null && donation.Agree)
                    {
                        Donation.AddDonationAnoniemFull(donation,id);
                       
                        //Returneer naar index pagina
                        return RedirectToAction("ThankYou/" + donation.ProjectId, "Home");
                    }
                }
            }
            return View(donation);
        }
    }
}
